home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / SocketClasses / Client / client.m < prev    next >
Encoding:
Text File  |  1992-07-23  |  8.6 KB  |  198 lines

  1. /***************************************************************************
  2. *                                                                          *
  3. * client.m                                                                 *
  4. * Copyright 1992 by Nik A Gervae                                           *
  5. *                                                                          *
  6. * An example using the Objective-C classes (SktSocketManager, SktSocket,   *
  7. * and SktSocketUser) which implement a convenient interface to Berkeley    *
  8. * stream sockets under NeXTSTEP(r).  See the accompanying class            *
  9. * specifications (files with a .rtf or .spec suffix) for further           *
  10. * information.                                                             *
  11. *                                                                          *
  12. * NeXTSTEP is a registered trademark of NeXT Computer, Inc.                *
  13. *                                                                          *
  14. ****************************************************************************
  15. *                                                                          *
  16. * LICENSE                                                                  *
  17. *                                                                          *
  18. * This program is free software; you can redistribute it and/or modify     *
  19. * it under the terms of the GNU General Public License as published by     *
  20. * the Free Software Foundation.                                            *
  21. *                                                                          *
  22. * The program and this makefile are distributed in the hope that it will   *
  23. * be useful, but are provided "AS IS" AND WITHOUT ANY WARRANTY; without    *
  24. * any express or implied warranty of MERCHANTABILITY or FITNESS FOR A      *
  25. * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
  26. * Any use or distribution of the program and documentation must include    *
  27. * appropriate copyrights to acknowledge Nik A. Gervae and the Free         *
  28. * Software Foundation, Inc.                                                *
  29. *                                                                          *
  30. * You should have received a copy of the GNU General Public License        *
  31. * along with this program; if not, write to the Free Software              *
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
  33. *                                                                          *
  34. ****************************************************************************
  35. *                                                                          *
  36. * BUILDING WITHOUT A MAKEFILE                                              *
  37. *                                                                          *
  38. * Just cc -c this file, then cc the .o with SktSocket.o, SktSocketUser.o,  *
  39. * and util.o.                                                              *
  40. *                                                                          *
  41. ****************************************************************************
  42. *                                                                          *
  43. * VERSION HISTORY                                                          *
  44. *                                                                          *
  45. * Version numbers are simply dates in the form YYYYMMDD.  These represent  *
  46. * the date that version was finished.  Only significantly changed versions *
  47. * are reported here, or those versions requiring explanation of changes.   *
  48. * There may be many interim stages between dated versions.                 *
  49. *                                                                          *
  50. * DateVersion Primary Author  Notes                                        *
  51. * ----------- --------------- -------------------------------------------- *
  52. * 19920327    Nik A Gervae    First released version                       *
  53. *                                                                          *
  54. ***************************************************************************/
  55.  
  56. #import <stdio.h>
  57.  
  58. #import <fcntl.h>
  59. #import <sys/types.h>
  60. #import <sys/time.h>
  61.  
  62. #import "SktSocket.h"
  63. #import "SktSocketUser.h"
  64.  
  65. /***************************************************************************
  66. *                                                                          *
  67. * These are the constant strings used.  Feel free to translate them into   *
  68. * your favorite language.  Do be sure to keep all the % directives in      *
  69. * place, or change the code that accesses these strings.                   *
  70. *                                                                          *
  71. ***************************************************************************/
  72. #define STR_Usage            "Usage: %s hostname port#\n"
  73. #define STR_ConnectionClosed "Connection closed\n"
  74.  
  75.  
  76. /***************************************************************************
  77. *                                                                          *
  78. * The User class                                                           *
  79. *                                                                          *
  80. * This is a very simple subclass of SktSocketUser, providing only the      *
  81. * minimum of methods to get anything at all done.                          *
  82. *                                                                          *
  83. ***************************************************************************/
  84.  
  85. @interface User : SktSocketUser
  86. {
  87. }
  88. - update;
  89.  
  90. @end
  91.  
  92. @implementation User
  93.  
  94. /***************************************************************************
  95. *                                                                          *
  96. * -update                                                                  *
  97. *                                                                          *
  98. * Flush out any input we have from the server, and see if the user has     *
  99. * typed anything in; if so, send it off to the server.                     *
  100. *                                                                          *
  101. ***************************************************************************/
  102. - update
  103. {
  104.   char     *input;            // pending input
  105.   long int  amount;           // amount of that input
  106.  
  107.   fd_set         readFds;     // for select()
  108.   struct timeval timeout;     // for select()
  109.   char           buffer[80];  // for read from input
  110.   int            readres;     // how much was read
  111.  
  112.   amount = [self getAllInput:&input];
  113.   if (0 < amount) {
  114.     printf("%.*s", amount, input);
  115.     fflush(stdout);
  116.     free(input);
  117.   }
  118.  
  119.  /*
  120.   * There is an easier way to get input from the keyboard, but
  121.   * I figured as long as I've been using these functions in the
  122.   * socket classes, go crazy. :-)
  123.   *
  124.   * You'll have to totally replace this part if you want to
  125.   * snag user input to filter it for commands....
  126.   */
  127.   FD_ZERO(&readFds);
  128.   FD_SET(0, &readFds);
  129.   timeout.tv_sec = 0;
  130.   timeout.tv_usec = 0;
  131.   select(1, &readFds, (fd_set *)0, (fd_set *)0, &timeout);
  132.  
  133.   if (FD_ISSET(0, &readFds)) {
  134.     if ((readres = read(0, buffer, sizeof(buffer))) != -1) {
  135.       [self queueOutput:buffer ofLength:readres];
  136.     }
  137.   }
  138.  
  139.   return self;
  140. }
  141.  
  142. @end
  143.  
  144. /***************************************************************************
  145. *                                                                          *
  146. * main()                                                                   *
  147. *                                                                          *
  148. * Check args, try to init, and go into an infinite loop until the user     *
  149. * types ctrl-C or the server closes the connection.                        *
  150. *                                                                          *
  151. ***************************************************************************/
  152. main(int argc, char *argv[])
  153. {
  154.   id   socket;
  155.   id   user;
  156.   char buffer[80];
  157.  
  158.   if (argc < 3) {
  159.     fprintf(stderr, STR_Usage, argv[0]);
  160.     exit(2);
  161.   }
  162.  
  163.  /*
  164.   * Make a new socket and a new user,
  165.   * bomb if either fails to get made.
  166.   */
  167.   socket = [[SktSocket alloc] initOnHostname:argv[1] andPort:atoi(argv[2])];
  168.   user = [[User alloc] initWithSocket:socket];
  169.  
  170.   if (!socket || !user) {
  171.     exit(3);
  172.   }
  173.  
  174.   while (1) {
  175.  
  176.    /*
  177.     * This happens when the server closes the connection.
  178.     */
  179.     if (![socket readInput]) {
  180.       fprintf(stderr, STR_ConnectionClosed);
  181.       fflush(stderr);
  182.       exit(0);
  183.     }
  184.  
  185.    /*
  186.     * Just keep having the user update and the socket printing
  187.     * output to the user.
  188.     */
  189.     [user update];
  190.     [socket flushOutput];
  191.   }
  192.  
  193.   exit(0);
  194. }
  195.  
  196. /***************************************************************************
  197. ***************************************************************************/
  198.